package engine;

import util.FileReader;
import util.Timer;
import util.Util;

public class Prefs 
{

    private boolean fullscrn;
    public boolean fullscrn() 
    {
        return fullscrn;
    }
    
    private boolean vsync;
    public boolean vsync() 
    {
        return vsync;
    }
    
    private int windowW;
    public int windowW() 
    {
        return windowW;
    }
    
    private int windowH;
    public int windowH() 
    {
        return windowH;
    }
    
    private int antialias;
    public int antialias() 
    {
        return antialias;
    }
    
    private int fov;
    public int fov() 
    {
        return fov;
    }
    
    private float mouseSens;
    public float mouseSens() 
    {
        return mouseSens;
    }
    
    public Prefs() 
    {
        Timer ti = new Timer(true);
        if (!readFromFile())  // If readFromFile fails,
        {
            saveDefaultsToFile();
            readFromFile();
        }
        Util.console("[Engine]  Loaded Prefs:      " + Util.formatFloat(ti.stopAndReturnTime()) + " seconds");
    }

    private void saveDefaultsToFile() 
    {
        FileReader rtf = new FileReader();
        rtf.openNewWrite("prefs");
        rtf.write("fullscrn= false");
        rtf.write("\nvsync= false");
        rtf.write("\nwindowW= 1280");
        rtf.write("\nwindowH= 800");
        rtf.write("\nantialias= 0");
        rtf.write("\nfov= 65");
        rtf.write("\nmouseSens= 0.0012");
        rtf.closeWrite();
    }
    
    public void saveToFile() 
    {
        Timer ti = new Timer(true);
        FileReader rtf = new FileReader();
        rtf.openNewWrite("prefs");
        rtf.write("fullscrn= " + fullscrn);
        rtf.write("\nvsync= " + vsync);
        rtf.write("\nwindowW= " + windowW);
        rtf.write("\nwindowH= " + windowH);
        rtf.write("\nantialias= " + antialias);
        rtf.write("\nfov= " + fov);
        rtf.write("\nmouseSens= " + mouseSens);
        rtf.closeWrite();
        Util.console("[Engine]  Saved Prefs:       " + Util.formatFloat(ti.stopAndReturnTime()) + " seconds");
    }

    private boolean readFromFile() 
    {
        FileReader rtf = new FileReader();
        if (!rtf.openNewRead("prefs")) 
        {
            return false;
        } 
        else 
        {
            while (rtf.canRead()) 
            {
                String title = rtf.read();
                String data = rtf.read();
                if (title.equals("fullscrn=")) 
                {
                    fullscrn = Boolean.parseBoolean(data);
                } 
                else if (title.equals("vsync=")) 
                {
                    vsync = Boolean.parseBoolean(data);
                } 
                else if (title.equals("windowW=")) 
                {
                    windowW = Integer.parseInt(data);
                } 
                else if (title.equals("windowH=")) 
                {
                    windowH = Integer.parseInt(data);
                } 
                else if (title.equals("antialias=")) 
                {
                    antialias = Integer.parseInt(data);
                } 
                else if (title.equals("fov=")) 
                {
                    fov = Integer.parseInt(data);
                } 
                else if (title.equals("mouseSens=")) 
                {
                    mouseSens = Float.parseFloat(data);
                }
            }
            rtf.closeRead();
            return true;
        }
    }

}
